package com.lucenly.card.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lucenly.card.R;
import com.lucenly.card.activity.PayActivity;
import com.lucenly.card.utils.DensityUtil;
import com.siberiadante.toastutils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class HomeDialog extends Dialog{
    String money;
    public HomeDialog(@NonNull final Context context, String punchDeposit) {
        super(context, R.style.alert_dialog);
        setContentView(R.layout.dialog_home);
        final LinearLayout ll_data = findViewById(R.id.ll_data);
        TextView tv_cancel = findViewById(R.id.tv_cancel);
        TextView tv_sure = findViewById(R.id.tv_sure);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        money = "1";
        if(punchDeposit!=null){
            final List<String> moneys = new ArrayList<>();
            if(punchDeposit.contains(",")){
                String[] split = punchDeposit.split(",");
                for (String str:
                     split) {
                    moneys.add(str);
                }
            }else{
                moneys.add(punchDeposit);
            }
            money = moneys.get(0);
            if(ll_data.getChildCount()>0){
                ll_data.removeAllViews();
            }
            int size = moneys.size();
            for(int i = 0 ; i<size ; i++){
                View view = getLayoutInflater().inflate(R.layout.item_money, null);
                final TextView tv_money = view.findViewById(R.id.tv_money);
                tv_money.setText("支付"+moneys.get(i)+"元参与挑战");
                if(i==0){
                    tv_money.setBackgroundResource(R.drawable.home_select_shape);
                    tv_money.setTextColor(context.getResources().getColor(R.color.white));
                }else{
                    tv_money.setBackgroundResource(R.drawable.home_shape);
                    tv_money.setTextColor(context.getResources().getColor(R.color.black_text_color));
                }
                final int finalI = i;
                tv_money.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int count = ll_data.getChildCount();
                        for (int i = 0 ; i < count;i++){
                            TextView textView = (TextView) ll_data.getChildAt(i);
                            textView.setBackgroundResource(R.drawable.home_shape);
                            textView.setTextColor(context.getResources().getColor(R.color.black_text_color));
                        }
                        money = moneys.get(finalI);
                        tv_money.setBackgroundResource(R.drawable.home_select_shape);
                        tv_money.setTextColor(context.getResources().getColor(R.color.white));
                    }
                });
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                );
                params.setMargins(0, DensityUtil.dp2px(10), 0, 0);
                ll_data.addView(view,params);
            }

        }
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(money!=null&&!money.equals("")){
//                    Toast.makeText(context, "选中了"+money+"元", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, PayActivity.class);
                    intent.putExtra("amount",money);
                    context.startActivity(intent);
                    dismiss();
                }else{
                    ToastUtil.showSingletonShort("没有金额");
//                    Toast.makeText(context, "没有金额", Toast.LENGTH_SHORT).show();
                }
            }
        });
        initView(context);
    }

    private void initView(Context context) {
        LinearLayout lLayout_bg = (LinearLayout) findViewById(R.id.lLayout_bg);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        setCanceledOnTouchOutside(false);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        setCanceledOnTouchOutside(true);
    }
}
